package o2o.web.actions.sys;


import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.mongodb.BasicDBObject;
import o2o.business.sms.UserSMSInfoParams;
import o2o.config.dao.MongoDaoImpl;
import o2o.dao.PO.release.RecordsUserOperInfo;
import o2o.dao.PO.sys.SMSLog;
import o2o.task.sys.SmsLogUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


// 短信发送日志
@Controller
public class SmsLogController {


    // 查看日志记录
    @RequestMapping(method = RequestMethod.POST, value = "/sys/sms/log/list")
    @ResponseBody
    public String test_sms(Long startTime, Long endTime, String phone, Integer page, Integer size) {
        //发送短信
        if (ValidateUtil.isNotAbsInteger(page)) {
            page = 1;
        }
        if (ValidateUtil.isNotAbsInteger(size)) {
            size = 20;
        }

        // 没传时间，就默认最近三天的
        if (ValidateUtil.isNotAbsLong(startTime)) {
            startTime = DateUtils.getPreDaysOfTimestamp(3l);
        }
        if (ValidateUtil.isNotAbsLong(endTime)) {
            endTime = DateUtils.getCurrTime();
        }

        BasicDBObject likeQuery = new BasicDBObject();
        likeQuery.append("sendTime", new BasicDBObject("$lt", endTime));// 小于
        likeQuery.append("sendTime", new BasicDBObject("$gt", startTime));// 大于
        if (ValidateUtil.isNotNull(phone)) {
            likeQuery.append("phone", phone);
        }

        // 首先获取收藏/点赞的记录标识信息
        List<HashMap> list = MongoDaoImpl.getInstance().find(HashMap.class, "sms_log", "user_sms", likeQuery, page - 1, size, "saveTime", -1);

        return AjaxResult.successResult(list);
    }


    // 查看微服务日志记录
    @RequestMapping(method = RequestMethod.POST, value = "/sys/service/sms/log/list")
    @ResponseBody
    public String s_sms(String serviceCol, Long startTime, Long endTime, String phone, Integer page, Integer size) {
        //发送短信
        if (ValidateUtil.isNotAbsInteger(page)) {
            page = 1;
        }
        if (ValidateUtil.isNotAbsInteger(size)) {
            size = 20;
        }

        // 没传时间，就默认最近三天的
        if (ValidateUtil.isNotAbsLong(startTime)) {
            startTime = DateUtils.getPreDaysOfTimestamp(3l);
        }
        if (ValidateUtil.isNotAbsLong(endTime)) {
            endTime = DateUtils.getCurrTime();
        }

        BasicDBObject likeQuery = new BasicDBObject();
        likeQuery.append("time", new BasicDBObject("$lt", DateUtils.sdfDateTime.get().format(endTime)));// 小于
        likeQuery.append("time", new BasicDBObject("$gt", DateUtils.sdfDateTime.get().format(startTime)));// 大于
        if (ValidateUtil.isNotNull(phone)) {
            likeQuery.append("phone", phone);
        }

        // 首先获取收藏/点赞的记录标识信息
        List<HashMap> list = MongoDaoImpl.getInstance().find(HashMap.class, "records_sms_user", serviceCol, likeQuery, page - 1, size, "time", -1);

        return AjaxResult.successResult(list);
    }
}
